package com.ruoyi.project.yuwen.yuwenaudio.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.yuwen.yuwenaudio.domain.Audio;
import com.ruoyi.project.yuwen.yuwenaudio.service.IAudioService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课文朗读Controller
 *
 * @author shixiangwen
 * @date 2021-02-24
 */
@Controller
@RequestMapping("/yuwen/yuwenaudio")
public class AudioController extends BaseController {

    private String prefix = "yuwen/yuwenaudio";

    @Autowired
    private IAudioService audioService;

    @RequiresPermissions("yuwen:yuwenaudio:view")
    @GetMapping()
    public String yuwenaudio() {
        return prefix + "/yuwenaudio";
    }

    @RequiresPermissions("yuwen:yuwenaudio:view")
    @GetMapping("/userList/{kkuid}")
    public String userList(@PathVariable("kkuid") Integer kkuid, ModelMap mmap) {
        return prefix + "/useraudio";
    }


    /**
     * 查询课文朗读列表
     */
    @RequiresPermissions("yuwen:yuwenaudio:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Audio audio) {
        startPage();
        List<Audio> list = audioService.selectAudioList(audio);
        return getDataTable(list);
    }

    /**
     * 导出课文朗读列表
     */
    @RequiresPermissions("yuwen:yuwenaudio:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Audio audio) {
        List<Audio> list = audioService.selectAudioList(audio);
        ExcelUtil<Audio> util = new ExcelUtil<Audio>(Audio.class);
        return util.exportExcel(list, "yuwenaudio");
    }

    /**
     * 新增课文朗读
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存课文朗读
     */
    @RequiresPermissions("yuwen:yuwenaudio:add")
    @Log(title = "课文朗读", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Audio audio) {
        return toAjax(audioService.insertAudio(audio));
    }

    /**
     * 修改课文朗读
     */
    @GetMapping("/edit/{vid}")
    public String edit(@PathVariable("vid") String vid, ModelMap mmap) {
        Audio audio = audioService.selectAudioById(vid);
        mmap.put("audio", audio);
        return prefix + "/edit";
    }

    @GetMapping("/share/{vid}")
    public String share(@PathVariable("vid") String vid, ModelMap mmap) {
        Audio audio = audioService.selectAudioById(vid);
        mmap.put("uid", audio.getKkuid());
        mmap.put("vid", vid);
        return prefix + "/share";
    }

    /**
     * 修改保存课文朗读
     */
    @RequiresPermissions("yuwen:yuwenaudio:edit")
    @Log(title = "课文朗读", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Audio audio) {
        return toAjax(audioService.updateAudio(audio));
    }

    @RequiresPermissions("yuwen:yuwenaudio:edit")
    @Log(title = "课文朗读", businessType = BusinessType.UPDATE)
    @PostMapping("/updateStatus")
    @ResponseBody
    public AjaxResult updateStatus(String vid, Integer vstatus) {
        return toAjax(audioService.updateStatus(vid, vstatus));
    }

    @RequiresPermissions("dict:jieshiapprove:edit")
    @Log(title = "朗读审核", businessType = BusinessType.UPDATE)
    @PostMapping("/batchApprove")
    @ResponseBody
    public AjaxResult batchApprove(String ids) {

        String[] strIds = Convert.toStrArray(ids);
        int count = 0;
        for (String strId : strIds) {
            audioService.updateStatus(strId, 2);
            count += 1;
        }

        return toAjax(count > 0);
    }


    @RequiresPermissions("yuwen:yuwenaudio:VIEW")
    @GetMapping("/get")
    @ResponseBody
    public AjaxResult get(String vid) {
        return AjaxResult.success(audioService.selectAudioById(vid));
    }

    /**
     * 删除课文朗读
     */
    @RequiresPermissions("yuwen:yuwenaudio:remove")
    @Log(title = "课文朗读", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(audioService.deleteAudioByIds(ids));
    }
}
